<?php
/* SVN FILE: $Id: assessment.php 34 2009-10-17 13:42:12Z kenreily@gmail.com $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Recenseo: Online submission and reviewing for education
 * Copyright 2008-2009, Ken Reily
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * $Date: 2009-10-17 13:42:12 +0000 (Sat, 17 Oct 2009) $
 * $Revision: 34 $
 * $Author: kenreily@gmail.com $
 * $HeadURL: http://recenseo.googlecode.com/svn/trunk/app/models/assessment.php $
 */
class Assessment extends AppModel {
  var $name = 'Assessment';
  var $belongsTo = array('User','Submission');
  var $hasMany = array('AssessmentTestCase','AssessmentQualityAttribute','AssessmentRating');
  var $validate = 
    array('mins_est' => array('hours_rule_1' => array('rule' => 'notEmpty',
						      'message' => 'Minutes must be between 1 and 60.'),
			      'hours_rule_2' => array('rule' => 'numeric',
						      'message' => 'Minutes must be between 1 and 60.'),
			      'hours_rule_3' => array('rule' => array('comparison','>=',1),
						      'message' => 'Minutes must be between 1 and 60.'),
			      'hours_rule_4' => array('rule' => array('comparison','<=',60),
						      'message' => 'Minutes must be between 1 and 60.')),
	  'author_rating' => array('rule' => 'notEmpty')
	  );
  
  function getForCourse($course_id, $user_id)
  {
    return $this->query("SELECT Assessment.id, Assessment.user_id, Assessment.author_rating, Submission.id, Submission.user_id, Assignment.name, Assessment.submitted, Assessment.created " .
                          "FROM assessments AS Assessment " .
                          "JOIN submissions AS Submission ON Submission.id = Assessment.submission_id " . 
                          "JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
			  "JOIN users AS User ON User.id = Assessment.user_id " .
                         "WHERE Assignment.course_id = {$course_id} " .
			   "AND Assessment.user_id = {$user_id} " .
			   "AND now() > Assignment.due " .
                           "AND (EXISTS (SELECT id FROM submissions AS s WHERE s.assignment_id = Assignment.id AND s.user_id = {$user_id} AND s.active = 1) " . 
			        "OR User.role = 'Instructor' OR User.role = 'TA') " .
			"ORDER BY Assessment.submitted DESC, Submission.created");
                           
  }
  
  function getForEdit($id,$user_id)
  {
    $assessment =  $this->query("SELECT Assessment.id, Assessment.user_id, Assessment.submitted, Submission.id, Submission.user_id, Submission.file_name, Submission.file_type, Submission.file_size, Submission.comments, Assignment.name, Assignment.course_id, GradingOption.name " .
				"FROM assessments AS Assessment " .
				"JOIN submissions AS Submission ON Submission.id = Assessment.submission_id " .
				"JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
				"LEFT JOIN grading_options AS GradingOption ON GradingOption.id = Submission.grading_option_id " .
				"WHERE Assessment.id = {$id} " .
				"AND Assessment.user_id = {$user_id} LIMIT 1");

    if (empty($assessment)) {
      return array();
    } else {
      return $assessment[0];
    }
  }

  function getForView($id,$user_id)
  {
    $assessment =  $this->query("SELECT Assessment.id, Assessment.user_id, Assessment.submitted, Assessment.author_rating, Submission.id, Submission.user_id, Submission.file_name, Submission.file_type, Submission.file_size, Submission.comments, Assignment.name, Assignment.course_id, GradingOption.name " .
				"FROM assessments AS Assessment " .
				"JOIN submissions AS Submission ON Submission.id = Assessment.submission_id " .
				"JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
				"LEFT JOIN grading_options AS GradingOption ON GradingOption.id = Submission.grading_option_id " .
				"WHERE Assessment.id = {$id} " .
				"AND (Assessment.user_id = {$user_id} " .
				     "OR Submission.user_id = {$user_id}) LIMIT 1");

    if (empty($assessment)) {
      return array();
    } else {
      return $assessment[0];
    }
  }

  function getForRating($id,$user_id)
  {
    $assessment =  $this->query("SELECT Assessment.id, Assessment.user_id, Assessment.submitted, Assessment.author_rating, Submission.id, Submission.user_id, Submission.file_name, Submission.file_type, Submission.file_size, Submission.comments, Assignment.name, Assignment.course_id, GradingOption.name " .
				"FROM assessments AS Assessment " .
				"JOIN submissions AS Submission ON Submission.id = Assessment.submission_id " .
				"JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
				"LEFT JOIN grading_options AS GradingOption ON GradingOption.id = Submission.grading_option_id " .
				"WHERE Assessment.id = {$id} " .
				"AND Assessment.submitted IS NOT NULL " .
				"AND Submission.user_id = {$user_id} LIMIT 1");

    if (empty($assessment)) {
      return array();
    } else {
      return $assessment[0];
    }
  }

  function countForUserAssignment($user_id,$assignment_id)
  {
    $result = $this->query("SELECT COUNT(AssignedAssessment.id) " .
			   "FROM assigned_assessments AS AssignedAssessment " .
			   "JOIN submissions AS Submission ON Submission.user_id = AssignedAssessment.reviewee_user_id AND Submission.assignment_id = AssignedAssessment.assignment_id " .
			   "JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
			   "WHERE AssignedAssessment.reviewer_user_id = {$user_id} " .
			   "AND Submission.active = 1 " .
			   "AND Assignment.id = {$assignment_id} " .
                           "AND EXISTS (SELECT id FROM submissions AS s WHERE s.assignment_id = Assignment.id AND s.user_id = {$user_id} AND s.active = 1) " . 
			   "AND NOT EXISTS (SELECT a.id FROM assessments AS a JOIN submissions AS s ON s.id = a.submission_id WHERE a.user_id = {$user_id} AND s.user_id = AssignedAssessment.reviewee_user_id AND s.assignment_id = AssignedAssessment.assignment_id AND a.submitted IS NOT NULL) " .
			   "AND now() > Assignment.due");

    if (!empty($result)) 
    {
      return $result[0][0]['COUNT(AssignedAssessment.id)'];
    } 
    else
    {
      return $result;
    }
  }

  function countForUserCourse($user_id,$course_id)
  {
    $result = $this->query("SELECT COUNT(AssignedAssessment.id) " .
			   "FROM assigned_assessments AS AssignedAssessment " .
			   "JOIN submissions AS Submission ON Submission.user_id = AssignedAssessment.reviewee_user_id AND Submission.assignment_id = AssignedAssessment.assignment_id " .
			   "JOIN assignments AS Assignment ON Assignment.id = Submission.assignment_id " .
			   "WHERE AssignedAssessment.reviewer_user_id = {$user_id} " .
			   "AND Submission.active = 1 " .
			   "AND Assignment.course_id = {$course_id} " .
                           "AND EXISTS (SELECT id FROM submissions AS s WHERE s.assignment_id = Assignment.id AND s.user_id = {$user_id} AND s.active = 1) " . 
			   "AND NOT EXISTS (SELECT a.id FROM assessments AS a JOIN submissions AS s ON s.id = a.submission_id WHERE a.user_id = {$user_id} AND s.user_id = AssignedAssessment.reviewee_user_id AND s.assignment_id = AssignedAssessment.assignment_id AND a.submitted IS NOT NULL) " .
			   "AND now() > Assignment.due");

    if (!empty($result)) 
    {
      return $result[0][0]['COUNT(AssignedAssessment.id)'];
    } 
    else
    {
      return $result;
    }
  }

  function getForSubmission($submission_id,$user_id)
  {
    return $this->query("SELECT Assessment.id " .
			  "FROM assessments AS Assessment " .
			 "WHERE Assessment.user_id = {$user_id} " .
			   "AND Assessment.submission_id = {$submission_id}");
  }

  // Returns a random assessment for a particular user and assignment.
  // Primarily intended for random, objective rating of assessments.
  function getRandomForUserAssignment($user_id,$assignment_id)
  {
    $result = $this->query('SELECT Assessment.id, Submission.id, COUNT(AssessmentRating.id) AS count ' .
			   'FROM assessments AS Assessment ' . 
			   'JOIN submissions AS Submission ON Submission.id = Assessment.submission_id ' .
     			   'LEFT OUTER JOIN assessment_ratings AS AssessmentRating ON AssessmentRating.assessment_id = Assessment.id ' .
			   "WHERE Assessment.user_id <> $user_id " .
			   "AND Submission.user_id <> $user_id " .
			   "AND Submission.assignment_id = $assignment_id " .
			   "AND Assessment.submitted IS NOT NULL " .
			   "AND NOT EXISTS (SELECT id FROM assessment_ratings WHERE user_id = $user_id AND assessment_id = Assessment.id) " . 
			   'GROUP BY Assessment.id, Submission.id ' .
			   'ORDER BY COUNT(AssessmentRating.id), RAND() LIMIT 1');

    if (!empty($result))
    {
      return $result[0];
    }
    else
    {
      return $result;
    }
  }
}
?>